package cz.cvut.fit.web.nakupniSeznam.dao;


import javax.persistence.NoResultException;
import javax.persistence.Query;

import cz.cvut.fit.web.nakupniSeznam.data.Unit;

public class UnitDAO extends GenericDAO<Unit>{
	
	private static UnitDAO instance = null;

	public static UnitDAO getInstance() {
		if (instance == null) {
			instance = new UnitDAO();
		}
		return instance;
	}

	@Override
	protected String getAllQueryName() {
		return "unit.getAll";
	}

	@Override
	protected Class<Unit> getEntityClass() {
		return Unit.class;
	}
	
	@SuppressWarnings("unchecked")
	public java.util.List<Unit> getByOwner(String key) {
		Query q = this.em.createNamedQuery("unit.getByOwner");
		q.setParameter("owner", key);

		try {
			return (java.util.List<Unit>) q.getResultList();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	public Unit getByOwnerAndName(String key, String name) {
		Query q = this.em.createNamedQuery("unit.getByOwnerAndName");
		q.setParameter("owner", key);
		q.setParameter("name", name);

		try {
			return (Unit) q.getSingleResult();
		} catch (NoResultException ex) {
			return null;
		}
	}

}
